package BasicClass.Four;


public class Test_Five {
    public static void main(String[] args) {
        ListNode node = new ListNode(1);
        ListNode node1 = new ListNode(2);
        ListNode node2 = new ListNode(3);
        ListNode node3 = new ListNode(4);
        ListNode node4 = new ListNode(5);

        node.next = node1;
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;

        ListNode listNode = ReverseList(node);
        while (listNode!=null){
            System.out.print(listNode.val+"-->");
            listNode = listNode.next;
        }
    }

    /**
     * 输入一个链表，反转链表后，输出新链表的表头。
     * @param head
     * @return
     */
    public static ListNode ReverseList(ListNode head) {
        ListNode pre = null;
        ListNode cru = null;
        while (head!=null){
            cru = head.next;
            head.next = pre;
            pre = head;
            head = cru;
        }
        return pre;
    }

    public static class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }
}
